package com.example.dc.Controller;

import com.example.dc.utils.Result;
import jakarta.servlet.http.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/cookie")
public class CookieController {

    //设置Cookie
    @GetMapping("/setcookie")
    public Result setCookie(HttpServletResponse response){
        try{
            response.addCookie(new jakarta.servlet.http.Cookie("name","pencil"));
            return Result.success();
        }
        catch (Exception err){
            return Result.systemError();
        }
    }

    //获取Cookie
    @GetMapping("/getcookie")
    public Result getCookie(HttpServletRequest request){
        try{
            Cookie[] cookies = request.getCookies();
            for(Cookie cookie:cookies){
                if(cookie.getName().equals("name")){
                    System.out.println("name:"+cookie.getValue());
                }
            }
            return Result.success();
        }
        catch (Exception err){
            return Result.systemError();
        }
    }
}
